package models

import (
	"database/sql"

	"log"

	"strconv"

	_ "github.com/go-sql-driver/mysql"
)

func init() {
	//init
}

///api/v1/wwfw/ywsjtj/test返回测试数据
func (this *Sut_EChart) TestData() {
	var (
		t1 Sut_Series
		t2 Sut_Series
	)
	this.Title.Text = "分类汇总测试"
	this.Legend.Data = []string{"测试分类1", "测试分类2"}
	this.XAxis.Data = []string{"总数", "硬件故障"}
	t1.Name = []string{"测试分类1"}
	t1.Data = []int{23, 34}
	t2.Name = []string{"测试分类2"}
	t2.Data = []int{14, 44}
	this.Series = append(this.Series, t1)
	this.Series = append(this.Series, t2)
}

//按工单类型（故障分类）进行分类汇总
func (this *Sut_EChart) GetGZFLHZ(start_date string, end_date string) {
	var (
		t1     Sut_Series
		sqlstr string
		rows   *sql.Rows
		err    error
		d      int
		s      string
		zs     int //统计总数
	)
	sqlstr = "select coalesce(count(id),0) from wwfw_gd WHERE riqi >= ? AND riqi <= ?" //调用视图求出总数
	err = db.QueryRow(sqlstr, start_date, end_date).Scan(&zs)
	if err != nil {
		log.Println("查询wwfw_gd表，求日期段的统计总数时出错！", err)
		return
	}
	//sqlstr = "select tjsj,sys_name from v_wwfw_gd_GZFLHZ WHERE riqi >= ? AND riqi <= ?" //调用视图
	//select count(sys_name) AS `tjsj`,sys_name from (`wwfw_gd` left join `t1_sys` on((gzlx_id = sys_id))) WHERE riqi >= ? AND riqi <= ? group by gzlx_id
	sqlstr = "select coalesce(count(gzlx_id),0) AS `tjsj`,(select `sys_name` from t1_sys where (`gzlx_id` = `sys_id`)) as `gzlx_name` from `wwfw_gd` WHERE riqi >= ? AND riqi <= ? group by gzlx_id"
	rows, err = db.Query(sqlstr, start_date, end_date)
	if err != nil {
		log.Println("查询wwfw_gd时出错！", err)
		return
	}
	for rows.Next() {
		err = rows.Scan(&d, &s)
		this.XAxis.Data = append(this.XAxis.Data, s)
		t1.Data = append(t1.Data, d)
	}
	this.Legend.Data = t1.Name
	this.Series = append(this.Series, t1)
	this.Title.Text = "按工单类别(故障类型)统计，总数：" + strconv.Itoa(zs)
}

//按故障设备分类汇总
func (this *Sut_EChart) GetGZSBHZ(start_date string, end_date string) {
	var (
		t1     Sut_Series
		sqlstr string
		rows   *sql.Rows
		err    error
		d      int
		s      string
		zs     int //统计总数
	)
	sqlstr = "select coalesce(count(id),0) from wwfw_gd WHERE riqi >= ? AND riqi <= ?"
	err = db.QueryRow(sqlstr, start_date, end_date).Scan(&zs)
	if err != nil {
		log.Println("查询wwfw_gd表，求日期段的统计总数时出错！", err)
		return
	}
	this.Title.Text = "按设备类型统计，总数：" + strconv.Itoa(zs)
	sqlstr = "select coalesce(count(gzsb_id),0) AS tjsj,(select mingcheng from t1_shebei where (`sb_id` = `gzsb_id`)) AS `sb_name` from `wwfw_gd` WHERE riqi >= ? AND riqi <= ?  group by gzsb_id"
	rows, err = db.Query(sqlstr, start_date, end_date)
	for rows.Next() {
		err = rows.Scan(&d, &s)
		this.XAxis.Data = append(this.XAxis.Data, s)
		t1.Data = append(t1.Data, d)
	}
	this.Legend.Data = t1.Name
	this.Series = append(this.Series, t1)
}

//按经手人分类汇总
func (this *Sut_EChart) GetJSRHZ(start_date string, end_date string) {
	var (
		t1     Sut_Series
		sqlstr string
		rows   *sql.Rows
		err    error
		d      int
		s      string
		zs     int //统计总数
	)
	sqlstr = "select coalesce(count(id),0) from wwfw_gd WHERE riqi >= ? AND riqi <= ?"
	err = db.QueryRow(sqlstr, start_date, end_date).Scan(&zs)
	if err != nil {
		log.Println("查询wwfw_gd表，求日期段的统计总数时出错！", err)
		return
	}
	this.Title.Text = "按经手人统计，总数：" + strconv.Itoa(zs)
	sqlstr = "select count(`wwfw_gd`.`id`) AS tjsj,(select `rbac_users`.`userName` from `rbac_users` where (`wwfw_gd`.`jsr_id` = `rbac_users`.`userId`)) AS `name` from `wwfw_gd` WHERE riqi >= ? AND riqi <= ?  group by jsr_id"
	rows, err = db.Query(sqlstr, start_date, end_date)
	for rows.Next() {
		err = rows.Scan(&d, &s)
		this.XAxis.Data = append(this.XAxis.Data, s)
		t1.Data = append(t1.Data, d)
	}
	this.Legend.Data = t1.Name
	this.Series = append(this.Series, t1)
}

//按处理方式分类汇总
func (this *Sut_EChart_Pie) GetCLFSHZ(start_date string, end_date string) {
	var (
		t1     Sut_EChart_Pie_Series_Data
		sqlstr string
		rows   *sql.Rows
		err    error
		d      int
		s      string
		zs     int //统计总数
	)
	sqlstr = "select coalesce(count(id),0) from wwfw_gd WHERE riqi >= ? AND riqi <= ?"
	err = db.QueryRow(sqlstr, start_date, end_date).Scan(&zs)
	if err != nil {
		log.Println("处理方式汇总，查询wwfw_gd表，求日期段的统计总数时出错！", err)
		return
	}
	this.Title_text = "按处理方式统计，总数：" + strconv.Itoa(zs)
	sqlstr = "select count(`wwfw_gd`.`id`) AS tjsj,(select `t1_sys`.`sys_name` from `t1_sys` where (`wwfw_gd`.`clfs_id` = `t1_sys`.`sys_id`)) AS `name` from `wwfw_gd` WHERE riqi >= ? AND riqi <= ?  group by clfs_id"
	rows, err = db.Query(sqlstr, start_date, end_date)
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&d, &s)
		checkErr(err)
		this.Legend_data = append(this.Legend_data, s)
		t1.Name = s
		t1.Value = d
		this.Series_data = append(this.Series_data, t1)
	}

}
